﻿using System;
using System.Web.UI.WebControls;
using System.IO;
using System.Web;
using Biblioteca.Objetos;
using Pms.Utils;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.HtmlControls;


namespace SERIX
{
    public partial class MasterPageSERIX : System.Web.UI.MasterPage
    {
        Usuario usuAut;
        string emp;

        protected void Page_Load(object sender, EventArgs e)
        {
            // Código usado para que o menu do asp.net funcione no safari e no Chrome
            if (Request.UserAgent.IndexOf("AppleWebKit") > 0)
            {
                Request.Browser.Adapters.Clear();
            }

            MenuPrincipal.Visible = false;
            usuAut = (Usuario)Session["usuario"];
            if (usuAut != null)
            {
                lblNome.Visible = true;
                CTecnicos tec = CTecnicos.PesquisaporCodUsu(usuAut.Codigo, usuAut.Login);
                if (usuAut.Nome.Contains(" "))
                    lblNome.Text = tec.Mat_tec + " - " + usuAut.Nome.Substring(0, usuAut.Nome.IndexOf(" "));
                else
                    lblNome.Text = tec.Mat_tec + " - " + usuAut.Nome;
                int width = (lblNome.Text.Length * 9);
                lblNome.Width = Unit.Pixel(width);
                MenuPrincipal.Visible = true;
                emp = usuAut.Cod_Empresa;
                ExecutarPermissoesMenu(MenuPrincipal.Items);
            }
            else
            {
                DesabilitarMenu(MenuPrincipal.Items);
            }

            if(Session["desabilitamasterpage"] != null)
            {
                MenuPrincipal.Enabled = false;
                SiteMapPath1.Enabled = false;
            }
            try
            {
                if (Request["op"] != null)
                {
                    if (Request["op"].ToString() == "Sair")
                    {
                        string Jscript = @"<script language='javascript'>                                    
                                    window.close();
                                    </script>";
                        Page.ClientScript.RegisterClientScriptBlock(GetType(), "cliente", Jscript);
                    }
                }
            }
            catch { }

            if (Page.Request.CurrentExecutionFilePath.ToString().ToLower() == "/logged1.aspx")
            {
                MenuPrincipal.Visible = false; 
                lblNome.Visible = false;
            }
        }

        private void ExecutarPermissoesMenu(MenuItemCollection items)
        {
            List<MenuItem> items_para_remover = new List<MenuItem>();
            foreach (MenuItem item in items)
            {
                string url = item.NavigateUrl.ToUpper();
                if (!string.IsNullOrEmpty(url))
                {
                    
                    url = url.Remove(0, 2);                    
                    if (url.IndexOf('?') >= 0)
                    {
                        url = url.Remove(url.IndexOf('?'), url.Length - url.IndexOf('?'));
                    }
                    if (!item.Value.Equals("Seguranca"))
                    {
                        if ((BiblioPMS.VerifiquePermissao(url, BiblioPMS.aplicacaoSNG, usuAut.Login) == 'N') &&
                       (!item.Value.ToUpper().Equals("SAIR")) &&
                       (!item.Value.ToUpper().Equals("ALTERAR SENHA")))
                        {
                            items_para_remover.Add(item);
                        }
                    }
                    if (item.Value.Equals("Seguranca"))
                    {
                        url = url.Substring(0,(url.Length));
                        if ((BiblioPMS.VerifiquePermissao(url, BiblioPMS.aplicacaoSEG, usuAut.Login) == 'N'))
                        {
                            items_para_remover.Add(item);
                        }
                    }

                }
                if (item.ChildItems.Count > 0)
                {
                    ExecutarPermissoesMenu(item.ChildItems);
                }
            }
            foreach (MenuItem itemr in items_para_remover)
            {
                items.Remove(itemr);
            }
        }

        private void DesabilitarMenu(MenuItemCollection items)
        {
            List<MenuItem> indices = new List<MenuItem>();
            MenuItemCollection items_para_remover = new MenuItemCollection();
            foreach (MenuItem item in items)
            {
                indices.Add(item);
            }
            foreach (MenuItem itemr in indices)
            {
                items.Remove(itemr);
            }
        }

        protected void btnSeg_Click(object sender, EventArgs e)
        {
            Response.Redirect("ValoresOrganizacoesManut.aspx");
        }

        protected void MenuPrincipal_MenuItemDataBound(object sender, MenuEventArgs e)
        {

        }
    }
}
